package com.rocksea.app;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.rocksea.app.db.UpLoadingTable;
import com.rocksea.app.db.UploadingDbHelper;
import com.rocksea.app.utils.UiUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

import butterknife.ButterKnife;
import butterknife.InjectView;

import static com.rocksea.app.db.history.HistoryTable.JY_LOG_DATA;
import static com.rocksea.app.db.history.HistoryTable.JY_SAMPLE_DATA;
import static com.rocksea.app.db.history.HistoryTable.TEST_DY;
import static com.rocksea.app.db.history.HistoryTable.TEST_GY;
import static com.rocksea.app.db.history.HistoryTable.TEST_JZ;
import static com.rocksea.app.db.history.HistoryTable.TEST_SC;

/**
 * @author by Mrw_v @date on 2018/9/28
 */

public class UploadingListActivity extends AppCompatActivity
{

    @InjectView(R.id.planList)
    ListView mPlanList;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.uploading_list_activity_layout);
        EventBus.getDefault().register(this);
        ButterKnife.inject(this);
        ActionBar mActionBar = getSupportActionBar();
        if (mActionBar!=null)
        {
            mActionBar.setTitle("待上传列表");
            mActionBar.setHomeButtonEnabled(true);
            mActionBar.setDisplayHomeAsUpEnabled(true);
        }
        initData();
    }

    protected void initData()
    {
        EventBus.getDefault().post(new LoadingEvent());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case android.R.id.home:
                finish();
                break;
        }

        return super.onOptionsItemSelected(item);
    }


    @Override
    protected void onDestroy()
    {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    public class LoadingEvent
    {
        public LoadingEvent() {
        }
    }

    @Subscribe(threadMode = ThreadMode.ASYNC)
    public void loading(LoadingEvent loadingEvent)
    {
        String a = loadingEvent.toString();
        EventBus.getDefault().post(UploadingDbHelper.queryTable());
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLoadingCompete(ArrayList<UpLoadingTable> upLoadingTables)
    {
        if (upLoadingTables.size() == 0)
        {
            UiUtils.showToast("暂无待上传数据");
        }
        else
        {
            UploadingAdapter uploadingAdapter = new UploadingAdapter(upLoadingTables);
            mPlanList.setAdapter(uploadingAdapter);
        }
    }
    private class UploadingAdapter extends BaseAdapter
    {
        private ArrayList<UpLoadingTable> upLoadingTables;

        private UploadingAdapter(ArrayList<UpLoadingTable> upLoadingTables)
        {
            this.upLoadingTables = upLoadingTables;
        }

        @Override
        public int getCount()
        {
            return upLoadingTables.size();
        }

        @Override
        public Object getItem(int i)
        {
            return null;
        }

        @Override
        public long getItemId(int i)
        {
            return 0;
        }

        @Override
        public View getView(final int i, View view, ViewGroup viewGroup)
        {
            if (view == null)
                view = View.inflate(getBaseContext(),R.layout.uploading_item_layout,null);
            TextView planNum = (TextView) view.findViewById(R.id.planNum);
            TextView time = (TextView) view.findViewById(R.id.time);
            TextView operation = (TextView) view.findViewById(R.id.operation);
            TextView mode = (TextView) view.findViewById(R.id.mode);
            operation.setText("撤销");
            operation.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    // finish current activity and post event to FileCenterFragment fresh TextView planTv
                    if (i == 0)
                    {
                        UploadingDbHelper.makeUnloading(upLoadingTables.get(i).baseInfoId,upLoadingTables.get(i).mode,upLoadingTables.get(i).uuid);
                        UploadingTask.cancel();
                    }
                    else
                    {
                        UploadingDbHelper.delete(upLoadingTables.get(i).baseInfoId,upLoadingTables.get(i).mode,upLoadingTables.get(i).uuid);
                    }
                    upLoadingTables.remove(i);
                    notifyDataSetChanged();
                }
            });
            planNum.setText(upLoadingTables.get(i).planNum);
            time.setText(upLoadingTables.get(i).pileNo);
            switch (upLoadingTables.get(i).mode)
            {
                case TEST_DY:
                    mode.setText("低应变");
                    break;

                case TEST_GY:
                    mode.setText("高应变");
                    break;

                case TEST_JZ:
                    mode.setText("静载");
                    break;

                case TEST_SC:
                    mode.setText("声测");
                    break;

                case JY_SAMPLE_DATA:
                    mode.setText("静载数据");
                    break;

                case JY_LOG_DATA:
                    mode.setText("静载日志");
                    break;
            }
            return view;
        }
    }


}
